﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlServerCe;
//using System.Drawing;

namespace WoWDeckbuilder
{
    class CardViewer
    {
        // database class
        Database db = new Database();

        // variable
        List<Card> cards;
        List<Card> filtered;

        // constructor
        public CardViewer()
        {
            cards = new List<Card>();
            FillCards();
            ResetFilter();
        }
        // propertye
        public List<Card> Cards()
        {
            return filtered;
        }

        //
        // Card search methodes
        //

        /// <summary>
        /// Gets a sinle card bij cardnumber.
        /// </summary>
        /// <param name="number">Cardnumber</param>
        /// <returns>Card</returns>
        public Card GetCard(string number)
        {
            Card one = cards.Find(
                delegate(Card cr)
                {
                    return cr.CardNumber == number;
                });
            return one;
        }

        /// <summary>
        /// Gets all cards from one set.
        /// </summary>
        /// <param name="SetID">The setID.</param>
        /// <returns>List with cards</returns>
        public List<Card> GetSetCards(string SetID)
        {
            List<Card> set = new List<Card>();
            //ToDo: filter cards for sets
            return set;
        }
        
        //
        // Fill cardinfo methodes
        //

        /// <summary>
        /// Fills the list cards.
        /// </summary>
        private void FillCards()
        {
            SqlCeDataReader CardReader = db.GetData("Card", null, null, "CardNumber");
            while (CardReader.Read())
            {
                Card single;
                switch (CardReader["Type"].ToString())
                {
                    case "Ability":
                        single = FillAbility(CardReader["CardNumber"].ToString());
                        break;
                    case "Ally":
                        single = FillAlly(CardReader["CardNumber"].ToString());
                        break;
                    case "Hero":
                        single = FillHero(CardReader["CardNumber"].ToString());
                        break;
                    case "Equipment":
                        single = FillEquipment(CardReader["CardNumber"].ToString());
                        break;
                    case "Location":
                        single = FillLocation(CardReader["CardNumber"].ToString());
                        break;
                    case "Quest":
                        single = FillQuest(CardReader["CardNumber"].ToString());
                        break;
                    default:
                        single = new Card(CardReader["CardNumber"].ToString());
                        break;
                }
                single.Name = CardReader["Name"].ToString();
                single.Faction = CardReader["Faction"].ToString();
                single.Cost = CardReader["Cost"].ToString();
                //ToDo: convert string to enum
                single.Type = ParseType(CardReader["Type"].ToString());
                single.Ability = CardReader["Ability"].ToString();
                single.Text = CardReader["Text"].ToString();
                single.Image = CardReader["Image"].ToString();
                single.Class = CardReader["Class"].ToString();
                single.Subtype = CardReader["Subtype"].ToString();
                single.Rarety = CardReader["Rarety"].ToString();
                cards.Add(single);
            }
            db.Close();
        }

        /// <summary>
        /// Fills the ability info from the database bij Cardnumber.
        /// </summary>
        /// <param name="number">Cardnumber</param>
        /// <returns>Ability</returns>
        public Ability FillAbility(string number)
        {
            Ability nieuw = new Ability(number);
            nieuw.Type = CardType.Ability;
            SqlCeDataReader TypeReader = db.GetCardExtra(nieuw.Type.ToString(), null, nieuw.CardNumber);
            TypeReader.Read();
            nieuw.Restriction = TypeReader["Restriction"].ToString();
            nieuw.Talent = TypeReader["Spec"].ToString();
            return nieuw;
        }

        /// <summary>
        /// Fills the ally info from the database bij Cardnumber.
        /// </summary>
        /// <param name="number">Cardnumber</param>
        /// <returns>Ally</returns>
        public Ally FillAlly(string number)
        {
            Ally nieuw = new Ally(number);
            nieuw.Type = CardType.Ally;
            SqlCeDataReader TypeReader = db.GetCardExtra(nieuw.Type.ToString(), null, nieuw.CardNumber);
            TypeReader.Read();
            nieuw.Race = TypeReader["Race"].ToString();
            return nieuw;
        }

        /// <summary>
        /// Fills the hero info from the database bij Cardnumber.
        /// </summary>
        /// <param name="number">Cardnumber</param>
        /// <returns>Hero</returns>
        public Hero FillHero(string number)
        {
            Hero nieuw = new Hero(number);
            nieuw.Type = CardType.Hero;
            SqlCeDataReader TypeReader = db.GetCardExtra(nieuw.Type.ToString(), null, nieuw.CardNumber);
            TypeReader.Read();
            nieuw.Talent = TypeReader["Spec"].ToString();
            nieuw.Race = TypeReader["Race"].ToString();
            return nieuw;
        }

        /// <summary>
        /// Fills the equipment info from the database bij Cardnumber.
        /// </summary>
        /// <param name="number">Cardnumber</param>
        /// <returns>Equipment</returns>
        public Equipment FillEquipment(string number)
        {
            Equipment nieuw = new Equipment (number);
            nieuw.Type = CardType.Equipment;
            SqlCeDataReader TypeReader = db.GetCardExtra(nieuw.Type.ToString(), null, nieuw.CardNumber);
            TypeReader.Read();
            return nieuw;
        }

        /// <summary>
        /// Fills the location info from the database bij Cardnumber.
        /// </summary>
        /// <param name="number">Cardnumber</param>
        /// <returns>Location</returns>
        public Location FillLocation(string number)
        {
            Location nieuw = new Location(number);
            nieuw.Type = CardType.Location;
            SqlCeDataReader TypeReader = db.GetCardExtra(nieuw.Type.ToString(), null, nieuw.CardNumber);
            TypeReader.Read();
            return nieuw;
        }

        /// <summary>
        /// Fills the quest info from the database bij Cardnumber.
        /// </summary>
        /// <param name="number">Cardnumber</param>
        /// <returns>Quest</returns>
        public Quest FillQuest(string number)
        {
            Quest nieuw = new Quest(number);
            nieuw.Type = CardType.Quest;
            SqlCeDataReader TypeReader = db.GetCardExtra(nieuw.Type.ToString(), null, nieuw.CardNumber);
            TypeReader.Read();
            return nieuw;
        }

        /// <summary>
        /// Resets the filtered cards.
        /// </summary>
        public void ResetFilter()
        {
            filtered = cards;
        }

        /// <summary>
        /// Filters the filtered cards.
        /// </summary>
        /// <param name="filter">The filter tag.</param>
        /// <param name="stay">The creteria on which the cards will be filtered.</param>
        public void Filter(string filter, List<string> stay)
        {
            foreach (Card single in filtered)
            {
                switch (filter)
                {
                    case "Type":
                        break;
                    case "Race":
                        break;
                    case "Class":
                        break;
                    case "Faction":
                        break;
                    case "Reputation":
                        break;
                    case "Slot":
                        break;
                    case "Rarity":
                        break;
                    case "Other":
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// Checks the card on there cardtype.
        /// </summary>
        /// <param name="stay">The CardType</param>
        /// <param name="single">The Card</param>
        /// <returns>True|False</returns>
        private bool FilterType(CardType stay, Card single)
        {
            bool contain=false;
            if (single.Type == stay)
                contain = true;
            return contain;
        }

        /// <summary>
        /// Checks the card on there race.
        /// </summary>
        /// <param name="stay">The race name</param>
        /// <param name="single">The Card</param>
        /// <returns>True|False</returns>
        private bool FilterRace(string stay, Card single)
        {
            bool contain = false;
            switch (single.Type)
            {
                case CardType.Ally:
                    Ally ll = (Ally)single;
                    if (ll.Race == stay)
                        contain = true;
                    break;
                case CardType.Hero:
                    Hero hr = (Hero)single;
                    if (hr.Race == stay)
                        contain = true;
                    break;
            }
            return contain;
        }

        /// <summary>
        /// Parses the string to a CardType.
        /// </summary>
        /// <param name="type">(String) cardtype</param>
        /// <returns>Cardtype (by default CardType.Quest)</returns>
        private CardType ParseType(string type)
        {
            CardType result = CardType.Quest;
            switch (type)
            {
                case "Ability":
                    result = CardType.Ability;
                    break;
                case "Ally":
                    result = CardType.Ally;
                    break;
                case "Hero":
                    result = CardType.Hero;
                    break;
                case "Equipment":
                    result = CardType.Equipment;
                    break;
                case "Location":
                    result = CardType.Location;
                    break;
                case "Quest":
                    result = CardType.Quest;
                    break;
            }
            return result;
        }
    }
}
